<?php
/**
 * Created by PhpStorm.
 * User: daojia
 * Date: 2020/5/8
 * Time: 12:50
 */
namespace app\api\service\pay;
use app\api\service\Basics;
use app\api\model\pay\Order as OrderModel;
use app\api\model\Lottery as LotteryModel;
use app\api\model\User as UserModel;
use app\common\enum\pay\order\PayStatus as PayStatusEnum;
class PaySuccess extends Basics
{
    // 订单模型
    public $model;

    // 当前用户信息
    private $user;
    /**
     * 构造函数
     * PaySuccess constructor.
     * @param $orderNo
     * @throws \think\exception\DbException
     */
    public function __construct($orderNo)
    {
        // 实例化订单模型
        $this->model = OrderModel::getPayDetail($orderNo);
        $this->wxappId = $this->model['wxapp_id'];
    }
    /**
     * 获取订单详情
     * @return OrderModel|null
     */
    public function getOrderInfo()
    {
        return $this->model;
    }
    /**
     * 订单支付成功业务处理
     * @param int $payType 支付类型
     * @param array $payData 支付回调数据
     * @return bool
     */
    public function onPaySuccess($payType, $payData)
    {
        log_write([
            'describe' => '确实支付回调',
            'data' => 'SUCCESSSUCCESS'
        ]);
        return $this->model->transaction(function () use ($payType, $payData) {
            // 更新订单状态
//            $this->model->save([
//                'pay_status' => PayStatusEnum::SUCCESS,
//                'pay_time' => time(),
//                'transaction_id' => $payData['transaction_id']
//            ]);
            log_write([
                'order_no' => $this->model['order_no']
            ]);
            db('pay_order')->where(['order_no'=>$this->model['order_no']])->update([
                'pay_status' => PayStatusEnum::SUCCESS,
                'pay_time' => time(),
                'transaction_id' => $payData['transaction_id']
            ]);
            log_write([
                '回调成功' => $this->model['order_no']
            ]);

            // 获取用户信息
            if($this->model['user_id'] != 0) {
                $this->user = UserModel::detail($this->model['user_id']);
                //修改用户实际消费金额
                $this->user->setIncExpendMoney($this->model['pay_price']);
            }
            $describe = "线下支付赠送：{$this->model['order_no']}";
            if($this->model['gift_money'] > 0) {
                // 用户积分变动
                if($this->model['user_id']) {
                    $this->user->setIncPoints($this->model['gift_money'], $describe);
                    $userInfo = $this->user->detail(['user_id' => $this->model['user_id']]);
                    //发送模板消息
                    $this->sendPointsTmp($userInfo, $this->model['gift_money']);
                    //增加大转盘抽奖次数
                    $LotteryModel = new LotteryModel();
                    $LotteryModel->updateLotteryUser($payData['transaction_id']);
                }else{
                    db('user_points_log')->insert([
                        'user_pay_id' => $this->model['user_pay_id'],
                        'value' => $this->model['gift_money'],
                        'describe' => $describe,
                        'wxapp_id' => '10001',
                        'create_time' => time()
                    ]);
//                    $this->user->setIncPoints($this->model['gift_money'], $describe,$this->model['user_pay_id']);
                    db('user_pay')->where(['id'=>$this->model['user_pay_id']])->setInc('points',$this->model['gift_money']);
                }
            }
            return true;
        });
    }

    /**
     * 发送积分到账模板消息
     * @param $userInfo
     */
    public function sendPointsTmp($userInfo, $gift_money){

        $template_id = 'gIQnIDPmH2o1761I1xUyj8XowFDPqE5evdtXbs5rUZw';
        $url = '';
        $openid = $userInfo['openid'];
        $appid = 'wxca5a1f0a213b34b7';
        $pagepath = 'pages/index/index';

        $datas = [
            'first' => ['value' => "尊敬的客户您好，您的积分已经发送到您的宜品购账户，请查收！\n"],
            'keyword1' => ['value' => $userInfo['nickName']],
            'keyword2' => ['value' => date('Y年m月d日 H时i分s秒')],
            'keyword3' => ['value' => '商品购买---宜品购线下门店'],
            'keyword4' => ['value' => $gift_money.'积分'],
            'keyword5' => ['value' => $userInfo['points'].'积分'],
            'remark' => ['value' => "\n点击查看账户详情。\n您本次获得的积分可在宜品购平台直接抵现" ,'color' => '#ff0000']
        ];
        sendTmp($template_id, $openid, $datas,$appid, $pagepath, $url);
    }
}